package st.business.tepth.business.user.busnumber.workholder.maintain.pass.content;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.MaintainPassInfoBean;
import st.business.tepth.business.bean.MaintainWorkBean;
import st.business.tepth.business.bean.PersonBean;
import st.business.tepth.business.event.StartBrotherEvent;
import st.business.tepth.business.user.busnumber.workholder.maintain.work.content.standardwork.partrecord.PartRecordDelegate;
import st.business.tepth.business.user.busnumber.workholder.maintain.work.content.standardwork.workdetail.WorkDetailReadOnlyDelegate;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.recycler.BaseDecoration;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-04-19 14:45
 */

public class ContentDelegate extends MvpFragment<ContentPresenter> implements ContentContract.View {
    @BindView(R2.id.recycler_view_maintain)
    RecyclerView mRecyclerViewMaintain;
    @BindView(R2.id.recycler_view_repair)
    RecyclerView mRecyclerViewRepair;
    @BindView(R2.id.radio_group)
    RadioGroup mRadioGroup;
    private MaintainPassInfoBean mMaintainPassInfoBean;

    public static ContentDelegate newInstance(
            MaintainPassInfoBean maintainPassInfoBean) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("info", maintainPassInfoBean);
        ContentDelegate delegate = new ContentDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mMaintainPassInfoBean = bundle.getParcelable("info");
        }
    }

    @Override
    public ContentPresenter getPresenter() {
        return new ContentPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_maintain_pass_content;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_repair) {
                    showHideRecycler(mRecyclerViewRepair, mRecyclerViewMaintain);
                } else {
                    showHideRecycler(mRecyclerViewMaintain, mRecyclerViewRepair);
                }
            }
        });
        mRecyclerViewMaintain.setLayoutManager(new LinearLayoutManager(_mActivity));
        mRecyclerViewMaintain.addItemDecoration(
                BaseDecoration.create(_mActivity.getResources()
                        .getColor(R.color.fontColorWhite), 6));
        mRecyclerViewMaintain.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                MaintainWorkBean maintainWorkBean = (MaintainWorkBean) baseQuickAdapter.getItem(i);
                if (maintainWorkBean != null) {
                    if (i == 0) {
                        EventBus.getDefault().post(
                                new StartBrotherEvent(PartRecordDelegate.
                                        newInstance(0, 0,
                                                maintainWorkBean.getId(), 4)));
                    } else {
                        EventBus.getDefault().post(
                                new StartBrotherEvent(WorkDetailReadOnlyDelegate
                                        .newInstance(maintainWorkBean.getId(),
                                                personToString(mMaintainPassInfoBean.getWorkerList()))));
                    }
                }
            }
        });
        if (mMaintainPassInfoBean != null) {
            mRecyclerViewMaintain.setAdapter(new MaintainContentAdapter(R.layout
                    .item_maintain_pass_maintian_content,
                    mMaintainPassInfoBean.getRepairMaintenanceOperation()));
        }

        mRecyclerViewRepair.setLayoutManager(new LinearLayoutManager(_mActivity));
        if (mMaintainPassInfoBean != null) {
            RepairContentAdapter repairContentAdapter = new RepairContentAdapter(
                    R.layout.item_maintain_pass_repair_content,
                    mMaintainPassInfoBean.getPhenomenonList());
            mRecyclerViewRepair.setAdapter(repairContentAdapter);
            repairContentAdapter.addHeaderView(LayoutInflater.from(_mActivity)
                    .inflate(R.layout.item_head_maintain_pass_check, null));
        }
        showHideRecycler(mRecyclerViewMaintain, mRecyclerViewRepair);
    }

    private void showHideRecycler(RecyclerView show, RecyclerView hide) {
        show.setVisibility(View.VISIBLE);
        hide.setVisibility(View.GONE);
    }

    private String personToString(List<PersonBean> list) {
        StringBuilder name = new StringBuilder();
        if (list != null) {
            for (PersonBean personBean : list) {
                name.append(personBean.getUser_name()).append("(").append(personBean.getJob_no()).append(") ");
            }
        }
        return name.toString();
    }
}